home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / ip / nfs / nfswatch4.0 / RCS / nit.c,v < prev    next >
Encoding:
Text File  |  1993-03-01  |  11.0 KB  |  629 lines

  1. head    4.0;
  2. access;
  3. symbols;
  4. locks; strict;
  5. comment    @ * @;
  6.  
  7.  
  8. 4.0
  9. date    93.03.01.19.59.00;    author davy;    state Exp;
  10. branches;
  11. next    3.6;
  12.  
  13. 3.6
  14. date    93.02.24.17.44.45;    author davy;    state Exp;
  15. branches;
  16. next    3.5;
  17.  
  18. 3.5
  19. date    93.01.15.19.33.39;    author davy;    state Exp;
  20. branches;
  21. next    3.4;
  22.  
  23. 3.4
  24. date    93.01.15.14.34.33;    author davy;    state Exp;
  25. branches;
  26. next    3.3;
  27.  
  28. 3.3
  29. date    93.01.14.15.51.16;    author davy;    state Exp;
  30. branches;
  31. next    3.2;
  32.  
  33. 3.2
  34. date    93.01.13.20.18.17;    author davy;    state Exp;
  35. branches;
  36. next    3.1;
  37.  
  38. 3.1
  39. date    93.01.13.13.03.01;    author davy;    state Exp;
  40. branches;
  41. next    3.0;
  42.  
  43. 3.0
  44. date    91.01.23.08.23.14;    author davy;    state Exp;
  45. branches;
  46. next    1.4;
  47.  
  48. 1.4
  49. date    90.12.04.08.25.22;    author davy;    state Exp;
  50. branches;
  51. next    1.3;
  52.  
  53. 1.3
  54. date    90.12.04.08.11.40;    author davy;    state Exp;
  55. branches;
  56. next    1.2;
  57.  
  58. 1.2
  59. date    90.08.17.15.47.32;    author davy;    state Exp;
  60. branches;
  61. next    1.1;
  62.  
  63. 1.1
  64. date    88.11.29.11.20.47;    author davy;    state Released;
  65. branches;
  66. next    ;
  67.  
  68.  
  69. desc
  70. @NFSWATCH - monitor Network File System traffic on the network.
  71. @
  72.  
  73.  
  74. 4.0
  75. log
  76. @NFSWATCH Version 4.0.
  77. @
  78. text
  79. @#ifndef lint
  80. static char *RCSid = "$Header: /home/harbor/davy/system/nfswatch/RCS/nit.c,v 3.6 1993/02/24 17:44:45 davy Exp davy $";
  81. #endif
  82.  
  83. #include "os.h"
  84.  
  85. #ifdef USE_NIT
  86. /*
  87.  * nit.c - routines for messing with the network interface tap.
  88.  *
  89.  * David A. Curry
  90.  * Purdue University
  91.  * Engineering Computer Network
  92.  * 1285 Electrical Engineering Building
  93.  * West Lafayette, IN 47907-1285
  94.  * davy@@ecn.purdue.edu
  95.  *
  96.  * $Log: nit.c,v $
  97.  * Revision 3.6  1993/02/24  17:44:45  davy
  98.  * Added -auth mode, changes to -proc mode, -map option, -server option.
  99.  *
  100.  * Revision 3.5  1993/01/15  19:33:39  davy
  101.  * Miscellaneous cleanups.
  102.  *
  103.  * Revision 3.4  1993/01/15  14:34:33  davy
  104.  * Changed to handle the default network interface even if the loopback
  105.  * was ifconfig'd first.
  106.  *
  107.  * Revision 3.3  1993/01/14  15:51:16  davy
  108.  * Added FDDI code and device type calculation to NIT and SNOOP.  The FDDI
  109.  * stuff almost definitely won't work without modification on the SNOOP
  110.  * side; it still needs to be tested on the NIT side.
  111.  *
  112.  * Revision 3.2  1993/01/13  20:18:17  davy
  113.  * Put in OS-specific define scheme, and merged in Tim Hudson's code for
  114.  * SGI systems (as yet untested).
  115.  *
  116.  * Revision 3.1  1993/01/13  13:03:01  davy
  117.  * Fixed the SUNOS40 calculation, and also fixed to only use promiscuous
  118.  * mode when needed.
  119.  *
  120.  * Revision 3.0  1991/01/23  08:23:14  davy
  121.  * NFSWATCH Version 3.0.
  122.  *
  123.  * Revision 1.4  90/12/04  08:25:22  davy
  124.  * Fixed to automatically define SUNOS40.
  125.  * 
  126.  * Revision 1.3  90/12/04  08:11:40  davy
  127.  * Changed ifdef for SunOS 4.0.x.
  128.  * 
  129.  * Revision 1.2  90/08/17  15:47:32  davy
  130.  * NFSWATCH Version 2.0.
  131.  * 
  132.  * Revision 1.1  88/11/29  11:20:47  davy
  133.  * NFSWATCH Release 1.0
  134.  * 
  135.  */
  136. #include <sys/param.h>
  137. #include <sys/stropts.h>
  138. #include <sys/socket.h>
  139. #include <sys/ioctl.h>
  140. #include <sys/time.h>
  141. #include <sys/file.h>
  142. #include <net/if.h>
  143. #include <signal.h>
  144. #include <stdio.h>
  145.  
  146. #include <net/nit_if.h>
  147. #include <net/nit_buf.h>
  148.  
  149. #include "nfswatch.h"
  150. #include "externs.h"
  151.  
  152. #if NOFILE <= 64
  153. #define SUNOS40 1
  154. #endif
  155.  
  156. /*
  157.  * setup_nit_dev - set up the network interface tap.
  158.  */
  159. int
  160. setup_nit_dev(device)
  161. char **device;
  162. {
  163.     int n, s, fd;
  164.     u_int chunksz;
  165.     u_long if_flags;
  166.     char buf[BUFSIZ];
  167.     struct ifconf ifc;
  168.     struct strioctl si;
  169.     struct timeval timeout;
  170.     struct ifreq ifr, *ifrp;
  171.  
  172.     /*
  173.      * If the interface device was not specified,
  174.      * get the default one.
  175.      */
  176.     if (*device == NULL) {
  177.         /*
  178.          * Grab a socket.
  179.          */
  180.         if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
  181.             error("socket");
  182.             finish(-1);
  183.         }
  184.  
  185.         ifc.ifc_buf = buf;
  186.         ifc.ifc_len = sizeof(buf);
  187.  
  188.         /*
  189.          * See what devices we've got.
  190.          */
  191.         if (ioctl(s, SIOCGIFCONF, (char *) &ifc) < 0) {
  192.             error("ioctl: SIOCGIFCONF");
  193.             finish(-1);
  194.         }
  195.  
  196.         /*
  197.          * Take the first device we encounter.
  198.          */
  199.         ifrp = ifc.ifc_req;
  200.         for (n = ifc.ifc_len/sizeof(struct ifreq); n > 0; n--,ifrp++) {
  201.             /*
  202.              * Skip the loopback interface.
  203.              */
  204.             if (strcmp(ifrp->ifr_name, "lo0") == 0)
  205.                 continue;
  206.  
  207.             *device = savestr(ifrp->ifr_name);
  208.             break;
  209.         }
  210.  
  211.         (void) close(s);
  212.     }
  213.  
  214.     /*
  215.      * We want the ethernet in promiscuous mode if we're looking
  216.      * at nodes other than ourselves, and we want to know about 
  217.      * dropped packets.
  218.      */
  219.     if (allflag || dstflag)
  220.         if_flags = NI_DROPS | NI_PROMISC | NI_TIMESTAMP;
  221.     else
  222.         if_flags = NI_DROPS | NI_TIMESTAMP;
  223.  
  224.     /*
  225.      * Open the network interface tap.
  226.      */
  227.     if ((fd = open(NIT_DEV, O_RDONLY)) < 0) {
  228.         error("nit: open");
  229.         finish(-1);
  230.     }
  231.  
  232.     /*
  233.      * Arrange to get discrete messages.
  234.      */
  235.     if (ioctl(fd, I_SRDOPT, (char *) RMSGD) < 0) {
  236.         error("ioctl: I_SRDOPT");
  237.         finish(-1);
  238.     }
  239.  
  240.     /*
  241.      * Push and configure the nit buffering module.
  242.      */
  243.     if (ioctl(fd, I_PUSH, NIT_BUF) < 0) {
  244.         error("ioctl: I_PUSH NIT_BUF");
  245.         finish(-1);
  246.     }
  247.  
  248.     /*
  249.      * Set the read timeout.
  250.      */
  251.     timeout.tv_sec = 1;
  252.     timeout.tv_usec = 0;
  253.  
  254.     si.ic_cmd = NIOCSTIME;
  255.     si.ic_timout = INFTIM;
  256.     si.ic_len = sizeof(timeout);
  257.     si.ic_dp = (char *) &timeout;
  258.  
  259.     if (ioctl(fd, I_STR, (char *) &si) < 0) {
  260.         error("ioctl: I_STR NIOCSTIME");
  261.         finish(-1);
  262.     }
  263.  
  264.     /*
  265.      * Set the chunk size.
  266.      */
  267.     chunksz = NIT_CHUNKSIZE;
  268.  
  269.     si.ic_cmd = NIOCSCHUNK;
  270.     si.ic_len = sizeof(chunksz);
  271.     si.ic_dp = (char *) &chunksz;
  272.  
  273.     if (ioctl(fd, I_STR, (char *) &si) < 0) {
  274.         error("ioctl: I_STR NIOCSCHUNK");
  275.         finish(-1);
  276.     }
  277.  
  278.     /*
  279.      * Configure the network interface tap by binding it
  280.      * to the underlying interface, setting the snapshot
  281.      * length, and setting the flags.
  282.      */
  283.     (void) strncpy(ifr.ifr_name, *device, sizeof(ifr.ifr_name));
  284.     ifr.ifr_name[sizeof(ifr.ifr_name)-1] = '\0';
  285.  
  286.     si.ic_cmd = NIOCBIND;
  287.     si.ic_len = sizeof(ifr);
  288.     si.ic_dp = (char *) 𝔦
  289.  
  290.     /*
  291.      * If the bind fails, there's no such device.
  292.      */
  293.     if (ioctl(fd, I_STR, (char *) &si) < 0) {
  294.         close(fd);
  295.         return(-1);
  296.     }
  297.  
  298.     /*
  299.      * SNAP is buggy on SunOS 4.0.x
  300.      */
  301. #ifndef SUNOS40
  302.     si.ic_cmd = NIOCSSNAP;
  303.     si.ic_len = sizeof(truncation);
  304.     si.ic_dp = (char *) &truncation;
  305.  
  306.     if (ioctl(fd, I_STR, (char *) &si) < 0) {
  307.         error("ioctl: I_STR NIOCSSNAP");
  308.         finish(-1);
  309.     }
  310. #endif
  311.  
  312.     si.ic_cmd = NIOCSFLAGS;
  313.     si.ic_len = sizeof(if_flags);
  314.     si.ic_dp = (char *) &if_flags;
  315.  
  316.     if (ioctl(fd, I_STR, (char *) &si) < 0) {
  317.         error("ioctl: I_STR NIOCSFLAGS");
  318.         finish(-1);
  319.     }
  320.  
  321.     return(fd);
  322. }
  323.  
  324. /*
  325.  * flush_nit - flush data from the nit.
  326.  */
  327. void
  328. flush_nit(fd)
  329. int fd;
  330. {
  331.     if (ioctl(fd, I_FLUSH, (char *) FLUSHR) < 0) {
  332.         error("ioctl: I_FLUSH");
  333.         finish(-1);
  334.     }
  335. }
  336.  
  337. /*
  338.  * nit_devtype - determine the type of device we're looking at.
  339.  */
  340. int
  341. nit_devtype(device)
  342. char *device;
  343. {
  344.     /*
  345.      * This whole routine is a kludge.  Ultrix does it the
  346.      * right way; see pfilt.c.
  347.      */
  348.  
  349.     if (strncmp(device, "le", 2) == 0 || strncmp(device, "ie", 2) == 0)
  350.         return(DLT_EN10MB);
  351.     
  352.     if (strncmp(device, "fddi", 4) == 0)
  353.         return(DLT_FDDI);
  354.  
  355.     fprintf(stderr, "Unknown device type: %s -- assuming ethernet.\n",
  356.         device);
  357.     return(DLT_EN10MB);
  358. }
  359. #endif /* USE_NIT */
  360. @
  361.  
  362.  
  363. 3.6
  364. log
  365. @Added -auth mode, changes to -proc mode, -map option, -server option.
  366. @
  367. text
  368. @d2 1
  369. a2 1
  370. static char *RCSid = "$Header: /home/harbor/davy/system/nfswatch/RCS/nit.c,v 3.5 1993/01/15 19:33:39 davy Exp davy $";
  371. d19 3
  372. @
  373.  
  374.  
  375. 3.5
  376. log
  377. @Miscellaneous cleanups.
  378. @
  379. text
  380. @d2 1
  381. a2 1
  382. static char *RCSid = "$Header: /home/harbor/davy/system/nfswatch/RCS/nit.c,v 3.4 1993/01/15 14:34:33 davy Exp davy $";
  383. d19 3
  384. d139 1
  385. a139 1
  386.         if_flags = NI_DROPS | NI_PROMISC;
  387. d141 1
  388. a141 1
  389.         if_flags = NI_DROPS;
  390. @
  391.  
  392.  
  393. 3.4
  394. log
  395. @Changed to handle the default network interface even if the loopback
  396. was ifconfig'd first.
  397. @
  398. text
  399. @d2 1
  400. a2 1
  401. static char *RCSid = "$Header: /home/harbor/davy/system/nfswatch/RCS/nit.c,v 3.3 1993/01/14 15:51:16 davy Exp davy $";
  402. d12 5
  403. a16 4
  404.  * SRI International
  405.  * 333 Ravenswood Avenue
  406.  * Menlo Park, CA 94025
  407.  * davy@@erg.sri.com
  408. d19 4
  409. d70 1
  410. a70 1
  411. #endif /* NOFILE */
  412. d226 1
  413. a226 1
  414. #endif /* SUNOS40 */
  415. @
  416.  
  417.  
  418. 3.3
  419. log
  420. @Added FDDI code and device type calculation to NIT and SNOOP.  The FDDI
  421. stuff almost definitely won't work without modification on the SNOOP
  422. side; it still needs to be tested on the NIT side.
  423. @
  424. text
  425. @d2 1
  426. a2 1
  427. static char *RCSid = "$Header: /home/harbor/davy/system/nfswatch/RCS/nit.c,v 3.2 1993/01/13 20:18:17 davy Exp davy $";
  428. d18 5
  429. d74 1
  430. a74 1
  431.     int s, fd;
  432. a75 1
  433.     char *strdup();
  434. a77 1
  435.     struct ifreq ifr;
  436. d81 1
  437. d100 1
  438. a100 1
  439.          * See what device it's attached to.
  440. d107 15
  441. a121 1
  442.         *device = strdup(ifc.ifc_req->ifr_name);
  443. @
  444.  
  445.  
  446. 3.2
  447. log
  448. @Put in OS-specific define scheme, and merged in Tim Hudson's code for
  449. SGI systems (as yet untested).
  450. @
  451. text
  452. @d2 1
  453. a2 1
  454. static char *RCSid = "$Header: /home/harbor/davy/system/nfswatch/RCS/nit.c,v 3.1 1993/01/13 13:03:01 davy Exp davy $";
  455. d18 4
  456. d228 23
  457. @
  458.  
  459.  
  460. 3.1
  461. log
  462. @Fixed the SUNOS40 calculation, and also fixed to only use promiscuous
  463. mode when needed.
  464. @
  465. text
  466. @d2 1
  467. a2 1
  468. static char *RCSid = "$Header: /home/harbor/davy/system/nfswatch/RCS/nit.c,v 3.0 1991/01/23 08:23:14 davy Exp davy $";
  469. d5 3
  470. a7 1
  471. #ifdef sun
  472. d18 4
  473. d225 1
  474. a225 1
  475. #endif /* sun */
  476. @
  477.  
  478.  
  479. 3.0
  480. log
  481. @NFSWATCH Version 3.0.
  482. @
  483. text
  484. @d2 1
  485. a2 1
  486. static char *RCSid = "$Header: /tmp_mnt/net/sparky.a/davy/system/nfswatch/RCS/nit.c,v 1.4 90/12/04 08:25:22 davy Exp Locker: davy $";
  487. d15 4
  488. a18 1
  489.  * $Log:    nit.c,v $
  490. d48 1
  491. a48 1
  492. #if NOFILE > 64
  493. d98 3
  494. a100 2
  495.      * We want the ethernet in promiscuous mode, and
  496.      * we want to know about dropped packets.
  497. d102 4
  498. a105 1
  499.     if_flags = NI_DROPS | NI_PROMISC;
  500. @
  501.  
  502.  
  503. 1.4
  504. log
  505. @Fixed to automatically define SUNOS40.
  506. @
  507. text
  508. @d2 1
  509. a2 1
  510. static char *RCSid = "$Header: /tmp_mnt/net/sparky.a/davy/progs/nfswatch/RCS/nit.c,v 1.3 90/12/04 08:11:40 davy Exp Locker: davy $";
  511. d13 1
  512. a13 1
  513.  * davy@@itstd.sri.com
  514. d16 3
  515. d52 1
  516. a52 1
  517. void
  518. d54 1
  519. a54 1
  520. char *device;
  521. d56 1
  522. a56 1
  523.     int s;
  524. d58 1
  525. d70 1
  526. a70 1
  527.     if (device == NULL) {
  528. d90 1
  529. a90 1
  530.         device = ifc.ifc_req->ifr_name;
  531. d103 1
  532. a103 1
  533.     if ((if_fd = open(NIT_DEV, O_RDONLY)) < 0) {
  534. d111 1
  535. a111 1
  536.     if (ioctl(if_fd, I_SRDOPT, (char *) RMSGD) < 0) {
  537. d119 1
  538. a119 1
  539.     if (ioctl(if_fd, I_PUSH, NIT_BUF) < 0) {
  540. d135 1
  541. a135 1
  542.     if (ioctl(if_fd, I_STR, (char *) &si) < 0) {
  543. d149 1
  544. a149 1
  545.     if (ioctl(if_fd, I_STR, (char *) &si) < 0) {
  546. d159 1
  547. a159 1
  548.     (void) strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
  549. d166 6
  550. a171 3
  551.     if (ioctl(if_fd, I_STR, (char *) &si) < 0) {
  552.         error("ioctl: I_STR NIOCBIND");
  553.         finish(-1);
  554. d182 1
  555. a182 1
  556.     if (ioctl(if_fd, I_STR, (char *) &si) < 0) {
  557. d192 1
  558. a192 1
  559.     if (ioctl(if_fd, I_STR, (char *) &si) < 0) {
  560. d196 2
  561. d204 2
  562. a205 1
  563. flush_nit()
  564. d207 1
  565. a207 1
  566.     if (ioctl(if_fd, I_FLUSH, (char *) FLUSHR) < 0) {
  567. @
  568.  
  569.  
  570. 1.3
  571. log
  572. @Changed ifdef for SunOS 4.0.x.
  573. @
  574. text
  575. @d2 1
  576. a2 1
  577. static char *RCSid = "$Header: /tmp_mnt/net/sparky.a/davy/progs/nfswatch/RCS/nit.c,v 1.2 90/08/17 15:47:32 davy Exp Locker: davy $";
  578. d16 3
  579. d41 4
  580. @
  581.  
  582.  
  583. 1.2
  584. log
  585. @NFSWATCH Version 2.0.
  586. @
  587. text
  588. @d2 1
  589. a2 1
  590. static char *RCSid = "$Header: /tmp_mnt/net/sparky.a/davy/system/nfswatch/RCS/nit.c,v 1.1 88/11/29 11:20:47 davy Released Locker: davy $";
  591. d16 3
  592. d161 1
  593. a161 1
  594.      * SNAP is buggy on 4.0.3.
  595. d163 1
  596. a163 1
  597. #ifndef _NIT_PF_        /* this is a 4.0.3 but not 4.1 symbol    */
  598. d172 1
  599. a172 1
  600. #endif /* _NIT_PF_ */
  601. @
  602.  
  603.  
  604. 1.1
  605. log
  606. @NFSWATCH Release 1.0
  607. @
  608. text
  609. @d2 1
  610. a2 1
  611. static char *RCSid = "$Header$";
  612. d5 1
  613. d10 4
  614. a13 5
  615.  * Research Institute for Advanced Computer Science
  616.  * Mail Stop 230-5
  617.  * NASA Ames Research Center
  618.  * Moffett Field, CA 94035
  619.  * davy@@riacs.edu
  620. d15 4
  621. a18 1
  622.  * $Log$
  623. d142 2
  624. a143 1
  625.      * to the unerlying interface and setting the flags.
  626. d157 14
  627. d192 1
  628. @
  629.